﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class SiteMaster : System.Web.UI.MasterPage
{
    PIMService pim = new PIMService();
    protected void Page_Load(object sender, EventArgs e)
    {
        string sPath = Request.Url.AbsoluteUri;
        //sPath = sPath.Substring(sPath.LastIndexOf("/") + 1);
        if (sPath.ToLower().Contains("index.aspx"))
        {
            imgHome.Visible = false;
        }

        if (!IsPostBack)
        {
            //if (!Request.Browser.IsMobileDevice)
            //{
            //    // string sPath = Request.Url.AbsoluteUri;
            //    string sPage = sPath.Substring(sPath.LastIndexOf("/") + 1);
            //    var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
            //    if (sPath.ToLower().Contains("mealplandetails.aspx") || sPath.ToLower().Contains("newrecipe.aspx?"))
            //    {
            //        Response.Redirect("../" + sPage);
            //    }
            //    else
            //        Response.Redirect("../default.aspx");
            //}
            try
            {
                if (Request.Cookies["PIMUserName"] != null && Request.Cookies["PIMPassword"] != null)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "error", "window.onload = loadcookie('" + Request.Cookies["PIMUserName"].Value + "','"
                                            + EncryptDecryptQueryString.Decrypt(Request.Cookies["PIMPassword"].Value) + "');", true);
                }
            }
            catch (Exception ex)
            {
                Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(-1);
            }

        }
        
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Login();
    }

    private void Login()
    {
        string url = string.Empty;
        try
        {
            //string result = pim.Login(username.Value, password.Value);
            //if (!string.IsNullOrEmpty(result))
            //{
                if (cbRemember.Checked)
                {
                    Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["PIMUserName"].Value = username.Value.Trim();
                    Response.Cookies["PIMPassword"].Value = EncryptDecryptQueryString.Encrypt(password.Value.Trim());
                }
                else
                {
                    Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(-1);

                }
                url = "index.aspx";
            //}
            //else
            //{
            //    ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "$('#dgLoginFail').dialog('open');", true);
            //}
        }
        catch (Exception ex)
        {

        }
        if (url.Length > 0)
            Response.Redirect(url);
    }

    protected void LogOut_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            pim.Logout();
            //Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(-1);
            //Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(-1);
        }
        catch (Exception ex)
        { 
            
        }
        Response.Redirect("index.aspx");
    }
    
}
